/**
 * Author: Todd Kerpelman
 * Date: 5/17/12
 * Time: 1:05 PM
 *
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package net.kerp.hangoutSpace {

import net.kerp.ToddUtils;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Point;

public class SpaceBackground extends Sprite{

  private var _width:int;
  private var _height:int;

  private static const NUM_OCTAVES:int = 2;
  private static const SEED:Number = Number(new Date());
  private static const CHANNELS:uint = BitmapDataChannel.BLUE;

  private var _scrollingBitmaps:Array;
  private var _wind:SpaceWind;

  [Embed(source="../../../../lib/testgradient.jpg")]
  private var imgClass:Class;

  public function SpaceBackground(width:int, height:int, wind:SpaceWind) {

    _width = width;
    _height = height;
    _wind = wind;
    _scrollingBitmaps = [];
    createStarfield();
    setupClouds();
    this.addEventListener(Event.ENTER_FRAME, onFrame);

  }


  private function createStarfield():void {
    var originalStarfieldData:BitmapData = new BitmapData(_width, _height, false, 0xFF000000);
    var starfieldBitmapData:BitmapData = originalStarfieldData.clone();

    for (var i:int=0; i<2800; i++) {

      var redLevel:int = 255 - (ToddUtils.randomInt(0, 64));
      var greenLevel:int = 255 - (ToddUtils.randomInt(0, 64));
      var blueLevel:int = 255 - (ToddUtils.randomInt(0, 64));
      var alphaLevel:int = 255 - (ToddUtils.randomInt(0,32));
      var finalColor:uint = (alphaLevel<<24) | (redLevel << 16) | (greenLevel << 8) | blueLevel;

      var pixelLoc:Point = new Point(ToddUtils.randomInt(0, _width), ToddUtils.randomInt(0, _height));
      originalStarfieldData.setPixel32(pixelLoc.x,  pixelLoc.y,  finalColor);

      // Every once in a while, create a big big star!
      if (Math.random() < 0.03) {
        originalStarfieldData.setPixel32(pixelLoc.x+1,  pixelLoc.y,  finalColor);
        originalStarfieldData.setPixel32(pixelLoc.x,  pixelLoc.y+1,  finalColor);
        originalStarfieldData.setPixel32(pixelLoc.x-1,  pixelLoc.y,  finalColor);
        originalStarfieldData.setPixel32(pixelLoc.x,  pixelLoc.y-1,  finalColor);
      }
    }
    originalStarfieldData.applyFilter(originalStarfieldData, originalStarfieldData.rect, new Point(0,0), new BlurFilter(2,2,1));
    this.addChild(new Bitmap(starfieldBitmapData));
    var starScrollingBitmap:ScrollingBitmap = new ScrollingBitmap(originalStarfieldData, starfieldBitmapData);
    starScrollingBitmap.scrollFactor = 0.35;
    _scrollingBitmaps.push(starScrollingBitmap);

  }


  private function setupClouds():void {
    var originalBitmapData:BitmapData = new BitmapData(_width, _height, true, 0x00000000);
    var cloudBitmapData:BitmapData = originalBitmapData.clone();
    var nebulaThingie:Bitmap = new imgClass;

    originalBitmapData.draw(nebulaThingie);

    // Apply some perlin noise to the gradient, to give it a nebula feel
    var noise:BitmapData = originalBitmapData.clone();
    noise.perlinNoise(120, 120, NUM_OCTAVES, SEED,
      true, true, 7, false);

    originalBitmapData.copyChannel(noise, noise.rect, new Point(0,0), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
    noise.dispose();

   this.addChild(new Bitmap(cloudBitmapData));
    var cloudScrollingBitmap:ScrollingBitmap = new ScrollingBitmap(originalBitmapData, cloudBitmapData);
    _scrollingBitmaps.push(cloudScrollingBitmap);
  }


  private function onFrame(event:Event):void {
    for each (var scroller:ScrollingBitmap in _scrollingBitmaps) {
      scroller.scrollTarget(-_wind.windX,  -_wind.windY);
    }
  }
  

}
}
